﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using iTextSharp.text.html.simpleparser;
using System.Collections;
using DTO;
using ModelLayer;

namespace BusinessLayer.Logic.ReportProducer
{
    public class PdfRadarProducer:IDisposable
    {
        Document document;
        MemoryStream ms;
        PdfWriter pw;

        public PdfRadarProducer()
        {
            document = new Document();
            ms = new MemoryStream();
            pw=PdfWriter.GetInstance(document, ms);
            document.OpenDocument();
        }

        private void addList(PdfPTable table, ProfiliDTO profilo, int row,String language,int mancanti)
        {
            PdfPCell cellListaCompentenzeMancanti = table.Rows[row].GetCells()[0];
            Paragraph parListaCompMancanti = (Paragraph)cellListaCompentenzeMancanti.CompositeElements[0];
            Font fontList = parListaCompMancanti.Chunks[0].Font;

            parListaCompMancanti.Clear();
            table.SplitLate = false;
            table.SplitRows = true;

            iTextSharp.text.List list = new iTextSharp.text.List(iTextSharp.text.List.UNORDERED, 10f);
            list.SetListSymbol("\u2022");
            list.IndentationLeft = 30f;

            foreach (ProfiloCompetenzaDTO profcomp in profilo.Competenze)
            {
                List<Competenza> competenze;
                if (mancanti==0)
                    competenze=profcomp.CompetenzeMancanti;
                else if (mancanti==1)
                    competenze = profcomp.CompetenzeMancantiMaSufficenti;
                else
                    competenze=profcomp.CompetenzeInPiu;

                foreach (Competenza comp in competenze)
                {
                    DictionaryManager m=DictionaryManager.Instance;
                    iTextSharp.text.ListItem li = new iTextSharp.text.ListItem(comp.Codice+" "+m.getString(language,comp.Codice), fontList);   
                    list.Add(li);
                }

            }
            cellListaCompentenzeMancanti.CompositeElements.Add(list);
        }

        public byte[] produce(byte[] image,String path,ReportRadarInfoDTO info)
        {
           
            
           
            iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();

            System.Collections.Generic.Dictionary<string, object> providers = new System.Collections.Generic.Dictionary<string, object>();
            iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
            providers.Add(HTMLWorker.IMG_BASEURL, path);
            hw.SetProviders(providers);
             Jpeg jpg = new Jpeg(image);
            jpg.ScaleAbsolute(300, 300);

            

            using (TextReader tx = File.OpenText(path+"/pdfReportRadar.htm"))
            {
                
                List<IElement> htmlarraylist =iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(tx, null,providers);
                IElement element = htmlarraylist[0];
                
                    PdfPTable table = (PdfPTable)element;

                    /* CREO IL TITOLO */
                    PdfPCell cellTitle = table.Rows[1].GetCells()[0];
                    Paragraph title = (Paragraph)cellTitle.CompositeElements[0];
                    Font fontTitle = title.Chunks[0].Font;
                    Chunk p1 = new Chunk("Report Di Prossimità al Profilo " + info.profili.NomeProfilo + " ("+((int)info.profili.PercentualeProssimita)+"%)" + Environment.NewLine +
                               "di " + info.utente.Nome + " " + info.utente.Cognome + Environment.NewLine +
                               "elaborato dal Cantiere dei Mestieri ICT il " + DateTime.Now.ToShortDateString(),fontTitle);                   

                    title.Clear();
                    title.Add(p1);

                    
                    //titleCompentenzeMancanti.Clear();
                    

                    /* CREO IL RADAR */
                    PdfPCell cellRadar=((PdfPTable)table.Rows[3].GetCells()[0].CompositeElements[0]).Rows[0].GetCells()[0];
                    cellRadar.AddElement(jpg);

                    /*CREO LA LISTA DI COMPETENZA*/
                    PdfPCell cellTitleCompentenzeMancanti = table.Rows[4].GetCells()[0];
                    ((Paragraph)cellTitleCompentenzeMancanti.CompositeElements[0]).Chunks[0].Append(" " + info.profili.NomeProfilo + ":");

                    PdfPCell cellTitleMancantiMaSufficenti = table.Rows[7].GetCells()[0];
                    ((Paragraph)cellTitleMancantiMaSufficenti.CompositeElements[0]).Chunks[0].Append(" " + info.profili.NomeProfilo + ":");

                    PdfPCell cellTitleCompentenzeEccedenti = table.Rows[10].GetCells()[0];
                    ((Paragraph)cellTitleCompentenzeEccedenti.CompositeElements[0]).Chunks[0].Append(" " + info.profili.NomeProfilo + ":");

                
                    addList(table, info.profili, 6,info.utente.Compagnia.Lingua,0);
                    addList(table, info.profili, 8, info.utente.Compagnia.Lingua, 1);
                    addList(table, info.profili, 12, info.utente.Compagnia.Lingua, 2);
                    
                  
                    
                   

                    document.Add(element);
            }

           
           // document.Add(jpg);
            document.Close();
            return ms.ToArray();
        }



        public void Dispose()
        {
            
            document.Close();
            pw.Close();
            ms.Close();
        }
    }
}
